/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.actions;

import com.google.inject.Inject;
import com.google.inject.Provider;
import listo.client.ContextManager;
import listo.client.ImageProvider;
import listo.client.Lang;
import listo.client.dialogs.TaskDialog;
import listo.client.model.Task;
import listo.client.model.operations.UpdateTasksOp;
import listo.client.tasklists.TaskList;
import listo.client.tasklists.TaskListManager;
import listo.utils.MiscUtils;
import listo.utils.guice.CustomScopes;
import listo.utils.logging.Log;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class EditTaskAction extends UpdateTasksAction implements TaskDialog.OkCancelListener<Task> {

    private final Provider<TaskDialog> taskDialogProvider;

    private Task task;
    private TaskDialog dialog;

    @Inject
    protected EditTaskAction(Log log, Lang lang, ImageProvider imageProvider, ContextManager contextManager,
                             TaskListManager taskListManager, Provider<TaskDialog> taskDialogProvider) {
        super(log, lang, "editTask", contextManager, taskListManager);
        this.taskDialogProvider = taskDialogProvider;

        putValue(Action.SMALL_ICON, imageProvider.getIcon("16x16/task_edit.png"));
        putValue(Action.LARGE_ICON_KEY, imageProvider.getIcon("22x22/task_edit.png"));
    }

    /**
     * Invoked when an action occurs.
     */
    public void perform(ActionEvent e, TaskList taskList) {
        task = (Task) taskList.getSelectedValue();

        // if we the dialog is currently open cancel it first
        if (dialog != null) dialog.cancel();

        dialog = taskDialogProvider.get();

        // check whether the user clicked on the note icon
        if (task.getNote() != null && "EditNote".equals(e.getActionCommand())) {
            dialog.setPanelCollapsedState(true, true);
        }

        dialog.setOkListener(this);
        dialog.setTitle("Edit task");
        dialog.run(task);
    }

    public void onOk(Task updatedTask) {
        onCancel(updatedTask);

        UpdateTasksOp op = new UpdateTasksOp();
        op.setTasks(task.getId());

        if (!MiscUtils.equals(task.getDesc(), updatedTask.getDesc())) op.addField("desc", updatedTask.getDesc());
        if (!MiscUtils.equals(task.getDue(), updatedTask.getDue())) op.addField("due", updatedTask.getDue());
        if (!MiscUtils.equals(task.getReminder(), updatedTask.getReminder())) {
            op.addField("reminder", updatedTask.getReminder());
        }
        String originalFolderString = task.getFolderIdsString();
        String updatedFolderString = updatedTask.getFolderIdsString();
        if (!MiscUtils.equals(originalFolderString, updatedFolderString)) op.addField("folders", updatedFolderString);
        if (!MiscUtils.equals(task.getNote(), updatedTask.getNote())) op.addField("note", updatedTask.getNote());

        contextManager.addAndRun(op);
    }

    public void onCancel(Task task) {
        dialog = null;
        CustomScopes.MANUAL.reset("TaskDialogScope");
    }

}